using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace DoItNow.UI
{
	public partial class IncrementalSearchForm : Form
	{
		public IncrementalSearchForm(string label, List<SearchEntry> searchEntries)
		{
			InitializeComponent();

			_searchLabel.Text = label;
			_searchResultsControl.SearchEntries = searchEntries;
		}

		public SearchEntry SearchResult
		{
			get { return _searchResultsControl.SelectedEntry; }
		}

		private void OnKeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape || e.Alt)
			{
				DialogResult = DialogResult.Cancel;
				Close();
			}

			if (e.KeyCode == Keys.Enter)
			{
				DialogResult = DialogResult.OK;
				Close();
			}

			if (e.KeyCode == Keys.Down)
			{
				_searchResultsControl.SelectNextResult();
				e.Handled = true;
			}

			if (e.KeyCode == Keys.Up)
			{
				_searchResultsControl.SelectPreviousResult();
				e.Handled = true;
			}
		}

		private void OnSearchTextChanged(object sender, EventArgs e)
		{
			_searchResultsControl.SearchText = _searchTextBox.Text;
		}
	}
}